<template>
	<view class="addshop">
		<view class="item proname">
			<view class="title">商品名称:</view>
			<input placeholder="输入商品名称" v-model="proname" class="inputitem"></input>
		</view>
		<view class="item barcode">
			<view class="title">条形码:</view>
			<view class="content">
				<input placeholder="输入条形码" v-model="barcode"></input>
				<image mode="aspectFit" src="../../static/saoyisao-2.png" class="sao" v-on:click="onScan()" v-if="isWxConfig==true"></image>
			</view>
		</view>
		<view class="item cost">
			<view class="title">进价:</view>
			<input type="digit" placeholder="输入进价" v-model="cost" class="inputitem"></input>
		</view>
		<view class="item price">
			<view class="title">售价:</view>
			<input type="digit" placeholder="输入售价" v-model="price" class="inputitem"></input>
		</view>
		<view class="item unit_name">
			<view class="title">单位:</view>
			<view class="inputitem" v-on:click="onUnit" v-if="unit_name.length==0">点击选择</view>
			<view class="inputitem" v-on:click="onUnit" v-if="unit_name.length>0">{{unit_name}}</view>
			<!-- <input type="digit" placeholder="输入单位" v-model="unit_name" class="input"></input> -->
		</view>
		<view class="item cate">
			<view class="title">分类:</view>
			<view class="inputitem" v-on:click="onCate" v-if="cateText.length==0">点击选择</view>
			<view class="inputitem" v-on:click="onCate" v-if="cateText.length>0">{{cateText}}</view>
			<!-- <input type="digit" placeholder="输入单位" v-model="unit_name" class="input"></input> -->
		</view>
		<view class="item barcode">
			<view class="title">内码:</view>
			<view class="content">
				<input placeholder="输入内码条形码(如果有)" v-model="sbarcode"></input>
				<image mode="aspectFit" src="../../static/saoyisao-2.png" class="sao" v-on:click="onScanscode()" v-if="isWxConfig==true"></image>
			</view>
		</view>
		<view class="item price">
			<view class="title">兑换数量:</view>
			<input type="digit" placeholder="可以兑换多少内码商品" v-model="exchange" class="inputitem"></input>
		</view>
		<view class="scanWrapper">
			<view class="scanA" v-if="isWxConfig==false">无法扫描</view>
			<view class="scanB" v-on:click="onScan" v-if="isWxConfig==true">扫描商品</view>
			<view class="scanB" v-on:click="onUpload">上架</view>
		</view>		
	</view>
</template>

<script>
	import http from "../../common/http.js"
	import common from "../../common/common.js"
	var app = getApp()
	export default {
		data() {
			return {
				storeid:0,
				isWxConfig:false,
				proname:'',
				barcode:'',
				cost:'',
				price:'',
				unit_name:'',
				id:0,
				isedit:false,
				sbarcode:'',
				exchange:0,
				cateText:'',
				cateItem:{},
				cateMap:{},
			}
		},
		onLoad(options) {
			this.storeid = options.storeid
			console.log(options)
			console.log(app.globalData.a)
			var self = this
			
			common.initWxConfigIfNeed(()=>{
				self.isWxConfig = true
			})
			
			http.get("/store/catetree", {}, (res)=> {
				self.cates = res
				var cateMap = {}
				for (var i=0;i<self.cates.length;i++) {
					cateMap[self.cates[i]["Id"]] = self.cates[i]["Name"]
				}
				self.cateMap = cateMap 
			})
		},
		onShow() {
			console.log(this.cateText)
			var cateItem = uni.getStorageSync("cate")
			console.log(cateItem)
			if (cateItem.length == 0) {return}
			// if (cateItem == void 0) {
			// 	return
			// }
			this.cateText = cateItem.Name
			uni.removeStorageSync("cate")
		},
		methods: {
			onScan() {
				// this.onScanFinished("6828804011173")
				// return

				var self = this
				common.scanQrCode((barcode)=>{
					
					http.get("/store/goods/add", {"barcode":barcode}, (res)=>{
						self.unit_name = res.UnitName
						self.proname = res.ProName
						self.barcode = res.BarCode
						self.sbarcode = res.Sbarcode
						self.cost = (res.Cost/100).toFixed(2)
						self.price = (res.Price/100).toFixed(2)
						console.log("xxxxxxx")
						self.cateText = self.cateMap[res.CateId]
					})
				})
			},
			onScanFinished(text) {
				
			},
			
			onUpload() {
				if (this.proname.length == 0) {
					uni.showToast({
						icon:'none',
						title:"请输入商品名称"
					})
					return
				}
				if (this.barcode.length == 0) {
					uni.showToast({
						icon:'none',
						title:"请输入商品条形码"
					})
					return
				}
				if (this.cost.length == 0) {
					uni.showToast({
						icon:'none',
						title:"请输入进价"
					})
					return
				}
				if (this.price.length == 0) {
					uni.showToast({
						icon:'none',
						title:"请输入售价"
					})
					return
				}
				if (this.unit_name.length == 0) {
					uni.showToast({
						icon:'none',
						title:"请选择单位"
					})
					return
				}
				if (this.cateText.length == 0) {
					uni.showToast({
						icon:'none',
						title:"请选择分类"
					})
					return
				}
				var data = {
					'proname':this.proname, 
					'barcode':this.barcode,
					'cost':this.cost*100,
					'price':this.price*100,
					'unit_name':this.unit_name,
					'storeid':this.storeid,
					'sbarcode':this.sbarcode,
					'exchange':this.exchange,
					'cateid':this.cateId
				}
				http.post("/store/goods/add", data, (r)=> {
					uni.showToast({
						title:"上架成功"
					})
				})
			},
			reset() {
				this.proname = ""
				this.barcode = ""
				this.cost = ""
				this.price = ""
				this.unit_name = ""
			},
			onUnit() {
				var self = this
				var arr = ["个", "只", "瓶", "件","盒","条","袋","箱"]
				uni.showActionSheet({
					itemList:arr,
					success(res){
						self.unit_name = arr[res.tapIndex]
					}
				})
			},
			onCate() {
				uni.navigateTo({
					url:"../cate/cate"
				})
			}
		}
	}
</script>

<style>

	.scanWrapper {
		margin-top: 10px;
		width: 100%;
		display: flex;
		justify-content: space-around;
	}
	.scanA {
		background-color: #DEDEDE;
		color: white;
		text-align: center;
		height: 44px;
		width: 40%;
		line-height: 44px;
		border-radius: 10px;
		font-size: 18px;
	}
	.scanB {
		background-color: #007AFF;
		color: white;
		text-align: center;
		height: 44px;
		width: 40%;
		line-height: 44px;
		border-radius: 10px;
		font-size: 18px;
	}
	.upload {
		background-color: #007AFF;
		color: white;
		text-align: center;
		height: 50px;
		width: 80%;
		line-height: 44px;
		border-radius: 10px;
		font-size: 18px;
	}
	.item {
		padding: 10px 15px;
		position: relative;
	}
	.item .title { 
		font-size: 18px;
		height: 35px;
	}
	.inputitem {
		background-color: #f3f3f2;
		height: 44px;
		padding: 0 10px;
		border-radius: 10px;
		line-height: 44px;
		color: #666666;
		box-sizing: border-box;
	}
	.content {
		height: 44px;
		line-height: 44px;
		width: 100%;
		position: relative;
	}
	.content input {
		background-color: #f3f3f2;
		height: 44px;
		padding: 0 10px;
		border-radius: 10px;
		line-height: 44px;
		color: #666666;
	}
	.sao {
		width: 25px;
		height: 25px;
		position: absolute;
		top: 10px;
		right: 10px;
	}
</style>
